package com.fable.web.controller.talent;

import com.alibaba.fastjson.JSON;
import com.fable.biz.operate.AdminRoleService;
import com.fable.biz.operate.AdminUserService;
import com.fable.biz.operate.CreateAdminUserModel;
import com.fable.biz.operate.CreateTalentModel;
import com.fable.common.config.RequireLogin;
import com.fable.common.utils.JsonResult;
import com.fable.dal.condition.AdminUserQueryCondition;
import com.fable.dal.dto.AdminUserDTO;
import com.fable.dal.mapper.ext.AdminUserExtMapper;
import com.fable.dal.mapper.ext.TalentExtMapper;
import com.fable.dal.model.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Created by weishuai on 18/7/21.
 */
@Controller
@RequireLogin
@RequestMapping("/talent")
public class TalentController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    TalentExtMapper talentExtMapper;

    @RequestMapping("/talentList.htm")
    public String list(ModelMap model) {
        TalentQuery query=new TalentQuery();
        query.setOrderByClause(" id desc ");
        query.createCriteria().andStatusGreaterThanOrEqualTo("0");
        List<TalentDO> list = talentExtMapper.selectByQuery(query);
        model.put("list", list);
        return "talent/talentList";
    }


    @RequestMapping("/talentAdd.htm")
    public String add(HttpServletRequest request, ModelMap model, Integer id) {
        //id不为空则说明是点击修改过来的

        if (id != null) {
            TalentDO talentDO = talentExtMapper.selectByPrimaryKey(id);
            model.put("result", talentDO);
        }
        return "talent/talentAdd";
    }

    @RequestMapping("/delTalent.json")
    @ResponseBody
    public JsonResult delete(HttpServletRequest request, Integer id) {
        JsonResult result = new JsonResult();
        if (id != null) {
            TalentDO talentDO=talentExtMapper.selectByPrimaryKey(id);
            if (talentDO == null) {
                result.setErrMsg("对象不存在!");
                return result;
            } else {
                talentDO.setStatus("-1");
                talentExtMapper.updateByPrimaryKeySelective(talentDO);
            }
        }
        return result;
    }


    @RequestMapping("/saveTalent.json")
    @ResponseBody
    public JsonResult saveAdminUser(@ModelAttribute CreateTalentModel createTalentModel) {
        JsonResult jsonResult = new JsonResult();
        try {

            if (createTalentModel.getId() != null) {
                TalentDO talentDO = talentExtMapper.selectByPrimaryKey(createTalentModel.getId());
                if (talentDO == null) {
                    logger.error("非法操作,param:{}", JSON.toJSONString(createTalentModel));
                    jsonResult.setErrMsg("非法操作!");
                    return jsonResult;
                }
                talentDO.setGmtModified(new Date());
                talentDO.setGmtCreate(new Date());
                BeanUtils.copyProperties(createTalentModel, talentDO);
                talentExtMapper.updateByPrimaryKeySelective(talentDO);
            } else {
                if (checkExist(createTalentModel)) {
                    jsonResult.setErrMsg("已存在相同手机号码用户!");
                    return jsonResult;
                }
                TalentDO talentDO = new TalentDO();
                BeanUtils.copyProperties(createTalentModel, talentDO);
                talentDO.setGmtModified(new Date());
                talentExtMapper.insertSelective(talentDO);
            }
        } catch (Exception e) {
            logger.error("保存人才异常,param:{}", JSON.toJSONString(createTalentModel), e);
            jsonResult.setErrMsg("保存异常请联系管理员!");
            return jsonResult;
        }
        return jsonResult;
    }


    private boolean checkExist(@ModelAttribute CreateTalentModel createTalentModel) {
        TalentQuery query = new TalentQuery();
        query.createCriteria().andStatusGreaterThanOrEqualTo("0").andMobileEqualTo(createTalentModel.getMobile());
        List<TalentDO> list = talentExtMapper.selectByQuery(query);
        if (!CollectionUtils.isEmpty(list)) {
            logger.warn("已存在候选人,param:{}", JSON.toJSONString(createTalentModel));
            return true;
        }
        return false;
    }

}
